// Fill out your copyright notice in the Description page of Project Settings.


#include "LoadData.h"
void ULoadData::Init()
{
}
TMap<int, FDirItem> ULoadData::LoadTextArray(FString path)
{
	TMap<int, FDirItem> dict_id_diritem;//

	TArray<FDirItem> dirs;
	//DirItem dirItem;
	TArray< FString> strs;
	FString fileParh = FPaths::ProjectContentDir() + path;
	bool isSucced = FFileHelper::LoadFileToStringArray(strs, *fileParh);
	if (isSucced)
	{

		for (int i = 0; i < strs.Num(); i++)
		{
			TArray< FString> splitTemp;
			TArray< FString> splitModelsnameTemp;
			strs[i].ParseIntoArray(splitTemp, TEXT(","));
			//
			FDirItem FDirItemtemp;
			FDirItemtemp.id = FCString::Atoi(*splitTemp[0]);
			FDirItemtemp.parent = FCString::Atoi(*splitTemp[1]);
			FDirItemtemp.cn_name = splitTemp[2];

			int num = splitTemp.Num();
			if (num > 3)
			{
				splitTemp[3].ParseIntoArray(splitModelsnameTemp, TEXT(" "));
				FDirItemtemp.filenames = splitModelsnameTemp;//youscapular_1_1_3_2 zuoscapular_1_1_3_2
			}
			//UE_LOG(LogTemp, Warning, TEXT("Hello World!%d"), num);
			dict_id_diritem.Add(FDirItemtemp.id, FDirItemtemp);
			dirs.Add(FDirItemtemp);
		}
	}
	return  dict_id_diritem;
}

FArrayData ULoadData::LoadTextArrayName(FString path)
{
	TMap<FString, FListDirItem> dict_name_diritem;
	TMap<int, FListDirItem> dict_id_childs;//
	TMap<int, FDirItem> dict_id_diritem = LoadTextArray(path);
	//dict_id_diritem_Get = dict_name_diritem;
	FArrayData FArrayData;
	for (auto Element : dict_id_diritem)
	{

		if (Element.Value.filenames.Num() > 0)
		{
			for (int i = 0; i < Element.Value.filenames.Num(); ++i)
			{
				FString name = Element.Value.filenames[i];
				if (name.IsEmpty())
					continue;
				if (!dict_name_diritem.Contains(name))
				{
					FListDirItem tempArray;
					dict_name_diritem.Add(name, tempArray);
				}
				dict_name_diritem[name].list.Add(Element.Value);
			}
		}
		else
		{
			FString name = Element.Value.filename;
			if (name.IsEmpty() == false)
			{
				if (!dict_name_diritem.Contains(name))
				{
					FListDirItem tempArray;
					dict_name_diritem.Add(name, tempArray);
				}
				dict_name_diritem[name].list.Add(Element.Value);
			}
		}
		if (!dict_id_childs.Contains(Element.Value.parent))
		{
			FListDirItem tempArray;
			dict_id_childs.Add(Element.Value.parent, tempArray);
		}
		else
		{
			dict_id_childs[Element.Value.parent].dict_id_childs.Add(Element.Value.id);

		}
		//UE_LOG(LogTemp, Warning, TEXT("ID: %d"), Element.Key)
	}
	FArrayData.dict_id_childs = dict_id_childs;
	FArrayData.dict_id_diritem = dict_id_diritem;
	FArrayData.dict_name_diritem = dict_name_diritem;
	return  FArrayData;
}
